<?php

class BaseView {

    private $_controlador;
    private $params;

    public function __construct(Request $peticion) {
        $this->_controlador = $peticion->getController();
        $this->params = new ParamsTransfer;
    }
    
    public function set_value($attribute_name, $value){
        $this->params->set_value($attribute_name, $value);
    }

    public function display($view, $item = false) {
        $this->params->set_value('controller_name', $this->_controlador);
        $this->params->set_value('css_tags', $this->css_tags());

        $this->params->display($this->content_path($view));
    }

    private function css_tags(){
        $str_css_tags = '';
        $css_directory_paths = array(
            VIEW_PATH . 'layout' . DS . DEFAULT_LAYOUT . DS . 'css' . DS . '*.css',
            VIEW_PATH . $this->_controlador . DS . 'css' . DS . '*.css',
            ROOT . 'public' . DS . 'css' . DS . '*.css');
        
        foreach($css_directory_paths as $directory){
            foreach (glob($directory) as $filename){
                $ref = str_replace(ROOT, BASE_URL, $filename);
                $str_css_tags .= '<link href="'. $ref . '" rel="stylesheet" type="text/css" />';
            }
        }
        return $str_css_tags;
    }
    
    private function content_path($view){
        $content_path = VIEW_PATH . $this->_controlador . DS . $view;

        if (is_readable($content_path . '.phtml')) {
            $content_path = $content_path . '.phtml';
            
        } elseif (is_readable($content_path . '.php')) {
            $content_path = $content_path . '.php';
        }

        else {
            throw new Exception('Error de vista');
        }
               
        return $content_path;
     }
    

}

?>
